import pygame
import numpy as np
import sys
from pygame.locals import *

# 初始化Pygame
pygame.init()

# 设置窗口
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞鸟动画")

# 确保中文正常显示
pygame.font.init()
font_options = ["AR PL UKai CN"]
font = None
for font_name in font_options:
    try:
        font = pygame.font.SysFont(font_name, 30)
        if font:
            break
    except:
        continue
if not font:
    font = pygame.font.SysFont(None, 30)  #  fallback

# 颜色设置
WHITE = (255, 255, 255)
SKY_BLUE = (135, 206, 235)

# 鸟的类
class Bird:
    def __init__(self):
        # 随机颜色
        self.body_color = tuple(np.random.randint(50, 200, 3))
        self.wing_color = tuple(np.random.randint(50, 200, 3))
        self.beak_color = (255, 165, 0)  # 喙的颜色固定为橙色
        
        # 位置和速度
        self.x = -50  # 从屏幕左侧外开始
        self.y = np.random.randint(100, HEIGHT - 100)
        self.speed = np.random.uniform(2, 5)
        
        # 翅膀动画参数
        self.wing_angle = 0
        self.wing_speed = np.random.uniform(0.1, 0.3)
        self.wing_direction = 1
        
    def update(self):
        # 更新位置
        self.x += self.speed
        if self.x > WIDTH + 50:  # 飞出屏幕后重置
            self.x = -50
            self.y = np.random.randint(100, HEIGHT - 100)
            self.speed = np.random.uniform(2, 5)
        
        # 更新翅膀角度（扇动效果）
        self.wing_angle += self.wing_speed * self.wing_direction
        if self.wing_angle > 0.8:
            self.wing_direction = -1
        elif self.wing_angle < -0.4:
            self.wing_direction = 1
    
    def draw(self, surface):
        # 绘制身体（椭圆）
        body_rect = pygame.Rect(0, 0, 60, 30)
        body_rect.center = (self.x, self.y)
        pygame.draw.ellipse(surface, self.body_color, body_rect)
        
        # 绘制头部（圆形）
        head_radius = 15
        pygame.draw.circle(surface, self.body_color, 
                          (int(self.x + 30), self.y), head_radius)
        
        # 绘制眼睛
        eye_pos = (int(self.x + 35), self.y - 5)
        pygame.draw.circle(surface, (0, 0, 0), eye_pos, 3)
        
        # 绘制喙（三角形）
        beak_points = [
            (self.x + 40, self.y),
            (self.x + 50, self.y - 3),
            (self.x + 50, self.y + 3)
        ]
        pygame.draw.polygon(surface, self.beak_color, beak_points)
        
        # 绘制翅膀（根据角度变化）
        wing_angle = self.wing_angle
        wing_points = [
            (self.x, self.y),
            (self.x - 30 * np.cos(wing_angle + np.pi/4), 
             self.y - 30 * np.sin(wing_angle + np.pi/4)),
            (self.x - 20 * np.cos(wing_angle - np.pi/4), 
             self.y + 20 * np.sin(wing_angle - np.pi/4))
        ]
        pygame.draw.polygon(surface, self.wing_color, wing_points)
        
        # 绘制尾巴
        tail_points = [
            (self.x - 30, self.y - 10),
            (self.x - 50, self.y),
            (self.x - 30, self.y + 10)
        ]
        pygame.draw.polygon(surface, self.body_color, tail_points)

# 创建鸟实例
bird = Bird()

# 主循环
clock = pygame.time.Clock()
running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
    
    # 清屏
    screen.fill(SKY_BLUE)
    
    # 更新和绘制鸟
    bird.update()
    bird.draw(screen)
    
    # 显示说明文字
    text = font.render("飞鸟动画 (按ESC退出)", True, (0, 0, 0))
    screen.blit(text, (10, 10))
    
    # 刷新屏幕
    pygame.display.flip()
    
    # 控制帧率
    clock.tick(60)
    
    # 检测ESC键退出
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        running = False

pygame.quit()
sys.exit()

